In [1]:
# CMSC 320
# Michael Dunphy 115379930

!pip install folium
import folium
import requests
import pandas
Requirement already satisfied: folium in /opt/conda/lib/python3.7/site-packages (0.10.1)
Requirement already satisfied: numpy in /opt/conda/lib/python3.7/site-packages (from folium) (1.17.0)
Requirement already satisfied: branca>=0.3.0 in /opt/conda/lib/python3.7/site-packages (from folium) (0.3.1)
Requirement already satisfied: jinja2>=2.9 in /opt/conda/lib/python3.7/site-packages (from folium) (2.10.1)
Requirement already satisfied: requests in /opt/conda/lib/python3.7/site-packages (from folium) (2.22.0)
Requirement already satisfied: six in /opt/conda/lib/python3.7/site-packages (from branca>=0.3.0->folium) (1.12.0)
Requirement already satisfied: MarkupSafe>=0.23 in /opt/conda/lib/python3.7/site-packages (from jinja2>=2.9->folium) (1.1.1)
Requirement already satisfied: idna<2.9,>=2.5 in /opt/conda/lib/python3.7/site-packages (from requests->folium) (2.8)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /opt/conda/lib/python3.7/site-packages (from requests->folium) (1.25.3)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.7/site-packages (from requests->folium) (2019.6.16)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /opt/conda/lib/python3.7/site-packages (from requests->folium) (3.0.4)
In [3]:
arrest_table = pandas.read_csv("http://www.hcbravo.org/IntroDataSci/misc/BPD_Arrests.csv")

arrest_table = arrest_table[pandas.notnull(arrest_table["Location 1"])]

arrest_table["lat"], arrest_table["long"] = arrest_table["Location 1"].str.split(",").str
arrest_table["lat"] = arrest_table["lat"].str.replace("(", "").astype(float)
arrest_table["long"] = arrest_table["long"].str.replace(")", "").astype(float)

arrest_table.head()
Out[3]:
arrest age race sex arrestDate arrestTime arrestLocation incidentOffense incidentLocation charge chargeDescription district post neighborhood Location 1 lat long
1 11127013.0 37 B M 01/01/2011 00:01:00 2000 Wilkens Ave 79-Other Wilkens Av & S Payson St 1 1425 Reckless Endangerment || Hand Gun Violation SOUTHERN 934.0 Carrollton Ridge (39.2814026274, -76.6483635135) 39.281403 -76.648364
2 11126887.0 46 B M 01/01/2011 00:01:00 2800 Mayfield Ave Unknown Offense NaN NaN Unknown Charge NORTHEASTERN 415.0 Belair-Edison (39.3227699160, -76.5735750473) 39.322770 -76.573575
3 11126873.0 50 B M 01/01/2011 00:04:00 2100 Ashburton St 79-Other 2100 Ashburton St 1 1106 Reg Firearm:Illegal Possession || Hgv WESTERN 735.0 Panway/Braddish Avenue (39.3117196723, -76.6623546313) 39.311720 -76.662355
4 11126968.0 33 B M 01/01/2011 00:05:00 4000 Wilsby Ave Unknown Offense 1700 Aliceanna St NaN Unknown Charge NORTHERN 525.0 Pen Lucy (39.3382885254, -76.6045667070) 39.338289 -76.604567
5 11127041.0 41 B M 01/01/2011 00:05:00 2900 Spellman Rd 81-Recovered Property 2900 Spelman Rd 1 1425 Reckless Endangerment || Handgun Violation SOUTHERN 924.0 Cherry Hill (39.2449886230, -76.6273582432) 39.244989 -76.627358
In [4]:
# To get a random sample of the data set

# Gives 1/10th of the data set that is randomly selected (Close to 6.4k elements)
sample = arrest_table.sample(frac=0.10, random_state=99)
sample.head()
Out[4]:
arrest age race sex arrestDate arrestTime arrestLocation incidentOffense incidentLocation charge chargeDescription district post neighborhood Location 1 lat long
19968 11222831.0 45 B M 05/20/2011 20:30:00 2100 Hoffman St 87-Narcotics 2100 E Hoffman St 4 3550 Cds:Possess-Not Marihuana || Cds Violation EASTERN 332.0 Broadway East (39.3067022998, -76.5881586469) 39.306702 -76.588159
85260 12528875.0 51 B M 08/08/2012 17:20:00 2800 W. Baltimore St 108-Liquor Law/Open Container 2800 W Baltimore St NaN Unknown Charge SOUTHWESTERN 816.0 Shipley Hill (39.2875786162, -76.6610127600) 39.287579 -76.661013
9845 11172047.0 45 W M 03/15/2011 18:30:00 2400 N. Charles St 6C-Larceny- Shoplifting 2400 N Charles St 1 0521 Theft Less Than $100.00 || Larceny NORTHERN 516.0 Charles Village (39.3164480398, -76.6168676958) 39.316448 -76.616868
90660 12550806.0 31 W M 09/12/2012 18:00:00 900 N Calhoun St 24-Towed Vehicle 900 N Calhoun St 4 3550 Cds:Possess-Not Marihuana || Poss Of Heroin WESTERN 713.0 Sandtown-Winchester (39.2990544990, -76.6401655178) 39.299054 -76.640166
30698 11282779.0 23 B M 07/29/2011 08:50:00 500 Laurens St 87-Narcotics 500 Laurens St 3 0233 Cds:P W/I Dist:Narc || Cds Violation CENTRAL 131.0 Druid Heights (39.3058768703, -76.6335255745) 39.305877 -76.633526
In [5]:
map_osm = folium.Map(location=[39.29, -76.61], zoom_start=11)

for index, row in sample.iterrows():
    
    # Changes the color of the circle based on Sex
    if row['sex'] == 'M':
        color='blue'
    else:
        color='red'
    
    # Pop-up info that can be seen by clicking on circle on map
    popup = folium.Popup('Age: ' + str(row['age']) + ' | Race: ' + str(row['race']) +
                        ' | Charge: ' + str(row['chargeDescription']))
    
    folium.Circle((row['lat'],row['long']), color = color, radius = 50., popup=popup).add_to(map_osm)
    
map_osm
Out[5]:
In [ ]:
# The map above shows the locations of a sample of the crimes committed in Balitmore. The color of the circles
# (red, blue) that represent each arrest, are based on the sex of the person arrested with blue representing male
# and red representing female. To learn more about each arrest, you can click on a circle in the map and a pop-up
# will appear showing the age and race of the person arrested as well as the charge. The sample gathered is 1/10th 
# the original data set and is randomly selected resulting in an accurate reflection of the real data set.